home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 3.2 / Ham Radio Version 3.2 (Chestnut CD-ROMs)(1993).ISO / packet / n17jsrc / ipdump.c < prev    next >
C/C++ Source or Header  |  1991-06-25  |  2KB  |  101 lines

  1. /* IP header tracing routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  /* Mods by PA0GRI */
  5. #include <stdio.h>
  6. #include "global.h"
  7. #include "config.h"
  8. #include "mbuf.h"
  9. #include "internet.h"
  10. #include "iface.h"
  11. #include "ip.h"
  12. #include "trace.h"
  13. #include "netuser.h"
  14.  
  15. void
  16. ip_dump(fp,bpp,check)
  17. FILE *fp;
  18. struct mbuf **bpp;
  19. int check;
  20. {
  21.     struct ip ip;
  22.     int16 ip_len;
  23.     int16 length;
  24.     int16 csum;
  25.  
  26.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  27.         return;    
  28.  
  29.     fprintf(fp,"IP:");
  30.     /* Sneak peek at IP header and find length */
  31.     ip_len = ((*bpp)->data[0] & 0xf) << 2;
  32.     if(ip_len < IPLEN){
  33.         fprintf(fp," bad header\n");
  34.         return;
  35.     }
  36.     if(check)
  37.         csum = cksum(NULLHEADER,*bpp,ip_len);
  38.     else
  39.         csum = 0;
  40.  
  41.     ntohip(&ip,bpp);    /* Can't fail, we've already checked ihl */
  42.  
  43.     /* Trim data segment if necessary. */
  44.     length = ip.length - ip_len;    /* Length of data portion */
  45.     trim_mbuf(bpp,length);    
  46.     fprintf(fp," len %u",ip.length);
  47.     fprintf(fp," %s",inet_ntoa(ip.source));
  48.     fprintf(fp,"->%s ihl %u ttl %u",
  49.         inet_ntoa(ip.dest),ip_len,uchar(ip.ttl));
  50.     if(ip.tos != 0)
  51.         fprintf(fp," tos %u",uchar(ip.tos));
  52.     if(ip.offset != 0 || ip.flags.mf)
  53.         fprintf(fp," id %u offs %u",ip.id,ip.offset);
  54.     if(ip.flags.congest)
  55.         fprintf(fp," CE");
  56.     if(ip.flags.df)
  57.         fprintf(fp," DF");
  58.     if(ip.flags.mf){
  59.         fprintf(fp," MF");
  60.         check = 0;    /* Bypass host-level checksum verify */
  61.     }
  62.     if(csum != 0)
  63.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  64.  
  65.     if(ip.offset != 0){
  66.         putc('\n',fp);
  67.         return;
  68.     }
  69.     switch(uchar(ip.protocol)){
  70.     case TCP_PTCL:
  71.         fprintf(fp," prot TCP\n");
  72.         tcp_dump(fp,bpp,ip.source,ip.dest,check);
  73.         break;
  74.     case UDP_PTCL:
  75.         fprintf(fp," prot UDP\n");
  76.         udp_dump(fp,bpp,ip.source,ip.dest,check);
  77.         break;
  78.     case ICMP_PTCL:
  79.         fprintf(fp," prot ICMP\n");
  80.         icmp_dump(fp,bpp,ip.source,ip.dest,check);
  81.         break;
  82.     case IP_PTCL:
  83.         fprintf(fp," prot IP\n");
  84.         ip_dump(fp,bpp,check);
  85.         break;
  86.     case AX25_PTCL:
  87.         fprintf(fp," prot AX25\n");
  88.         ax25_dump(fp,bpp,check);
  89.         break;
  90. #ifdef    RSPF
  91.     case RSPF_PTCL:
  92.         fprintf(fp," prot RSPF\n");
  93.         rspf_dump(fp,bpp,ip.source,ip.dest,check);
  94.         break;
  95. #endif
  96.     default:
  97.         fprintf(fp," prot %u\n",uchar(ip.protocol));
  98.         break;
  99.     }
  100. }
  101.